
package org.bridgen.lang.semantics.ast;
import java.util.*;



/* Patterns */


public class SearchPattern extends ASTNode<ASTNode> implements Cloneable {
    public void flushCache() {
        super.flushCache();
    }
     @SuppressWarnings({"unchecked", "cast"})  public SearchPattern clone() throws CloneNotSupportedException {
        SearchPattern node = (SearchPattern)super.clone();
        node.in$Circle(false);
        node.is$Final(false);
    return node;
    }
     @SuppressWarnings({"unchecked", "cast"})  public SearchPattern copy() {
      try {
          SearchPattern node = (SearchPattern)clone();
          if(children != null) node.children = (ASTNode[])children.clone();
          return node;
      } catch (CloneNotSupportedException e) {
      }
      System.err.println("Error: Could not clone node of type " + getClass().getName() + "!");
      return null;
    }
     @SuppressWarnings({"unchecked", "cast"})  public SearchPattern fullCopy() {
        SearchPattern res = (SearchPattern)copy();
        for(int i = 0; i < getNumChildNoTransform(); i++) {
          ASTNode node = getChildNoTransform(i);
          if(node != null) node = node.fullCopy();
          res.setChild(node, i);
        }
        return res;
    }
    // Declared in PrettyPrint.jrag at line 280

        
    public void pp(StringBuffer buf) {
    	buf.append("[");
    	getOrdering().pp(buf);
    	buf.append(" ");
    	getDirection().pp(buf);
    	buf.append(" ");
    	getNodeUse().pp(buf);
    	buf.append("]");
    }

    // Declared in TypeCodeGen.jrag at line 771
 
	
	public void generateCode(String indent, StringBuffer buf) {
		// code: LexicalNode pos = (getPrevious() | getNext());
		buf.append(indent + "LexicalNode pos = get" + (isLeft() ? "Previous" : "Next") + "();\n");
		// code: int order = ( 1 | 2 | 3);
		buf.append(indent + "int order = " + order() + ";\n");
		// code: int hits = 1;
		buf.append(indent + "int hits = 1;\n");
		// code: while (pos != null && hits <= order) {
		buf.append(indent + "while (pos != null && hits <= order) {\n");
		
		// code: Object find = pos.getPreviousOfType(Type.class) (or Next)
		buf.append(indent + INDENT + "Object find = pos.get" + 
			(isLeft() ? "Previous" : "Next") + "OfType(" + type() + ".class);\n");
		// code: if (find == null) {
		buf.append(indent + INDENT + "if (find == null) {\n");
		// code: pos = null;
		buf.append(indent + INDENT + indent + "pos = null;\n");
		// code: } else if (hits == order) {
		buf.append(indent + INDENT + "} else if (hits == order) {\n");
		// code: return (Type)find;
		buf.append(indent + INDENT + INDENT + "return (" + type() + ")find;\n");
		// code: } else {
		buf.append(indent + INDENT + "} else {\n");
		// code: hits++;
		buf.append(indent + INDENT + INDENT + "hits++;\n");
		// code: pos = ((Type)find).getPrevious(); (or Next)
		buf.append(indent + INDENT + INDENT + "pos = ((" + type() + 
			")find).get" + (isLeft() ? "Previous" : "Next") + "();\n");
		// code: }
		buf.append(indent + INDENT + "}\n");
		// code: }
		buf.append(indent + "}\n");
		// code: return null;
		buf.append(indent + "return null;\n");
	}

    // Declared in bridgit.ast at line 3
    // Declared in bridgit.ast line 74

    public SearchPattern() {
        super();


    }

    // Declared in bridgit.ast at line 10


    // Declared in bridgit.ast line 74
    public SearchPattern(Ordering p0, Direction p1, NodeUse p2) {
        setChild(p0, 0);
        setChild(p1, 1);
        setChild(p2, 2);
    }

    // Declared in bridgit.ast at line 16


  protected int numChildren() {
    return 3;
  }

    // Declared in bridgit.ast at line 19

  public boolean mayHaveRewrite() { return false; }

    // Declared in bridgit.ast at line 2
    // Declared in bridgit.ast line 74
    public void setOrdering(Ordering node) {
        setChild(node, 0);
    }

    // Declared in bridgit.ast at line 5

    public Ordering getOrdering() {
        return (Ordering)getChild(0);
    }

    // Declared in bridgit.ast at line 9


    public Ordering getOrderingNoTransform() {
        return (Ordering)getChildNoTransform(0);
    }

    // Declared in bridgit.ast at line 2
    // Declared in bridgit.ast line 74
    public void setDirection(Direction node) {
        setChild(node, 1);
    }

    // Declared in bridgit.ast at line 5

    public Direction getDirection() {
        return (Direction)getChild(1);
    }

    // Declared in bridgit.ast at line 9


    public Direction getDirectionNoTransform() {
        return (Direction)getChildNoTransform(1);
    }

    // Declared in bridgit.ast at line 2
    // Declared in bridgit.ast line 74
    public void setNodeUse(NodeUse node) {
        setChild(node, 2);
    }

    // Declared in bridgit.ast at line 5

    public NodeUse getNodeUse() {
        return (NodeUse)getChild(2);
    }

    // Declared in bridgit.ast at line 9


    public NodeUse getNodeUseNoTransform() {
        return (NodeUse)getChildNoTransform(2);
    }

    // Declared in TypeCodeGen.jrag at line 762
 @SuppressWarnings({"unchecked", "cast"})     public boolean isLeft() {
        boolean isLeft_value = isLeft_compute();
        return isLeft_value;
    }

    private boolean isLeft_compute() {  return getDirection().isLeft();  }

    // Declared in TypeCodeGen.jrag at line 763
 @SuppressWarnings({"unchecked", "cast"})     public String type() {
        String type_value = type_compute();
        return type_value;
    }

    private String type_compute() {  return getNodeUse().getName();  }

    // Declared in TypeCodeGen.jrag at line 766
 @SuppressWarnings({"unchecked", "cast"})     public int order() {
        int order_value = order_compute();
        return order_value;
    }

    private int order_compute() {  return getOrdering().asNbr();  }

    // Declared in PrettyPrint.jrag at line 20
 @SuppressWarnings({"unchecked", "cast"})     public String indent() {
        String indent_value = getParent().Define_String_indent(this, null);
        return indent_value;
    }

public ASTNode rewriteTo() {
    return super.rewriteTo();
}

}
